package com.unohacha.qqj.fzdr.view;


import android.app.Activity;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.ShareInfo;
import com.unohacha.qqj.fzdr.util.ActivityInfoStrings;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
 * 分享对话框
 * Created by wanghongchuang
 * on 2016/8/25.
 * email:844285775@qq.com
 */
public class ShareDialog extends Dialog implements View.OnClickListener {

    LinearLayout layContent;
    Button btnCancle;
    View vDismiss;
    UMWeb umWeb;

    private UMShareListener umShareListener;
    private Activity activity;

    List<ShareItem> shareList = new ArrayList<>();

    SHARE_MEDIA[] shareMedias;
    ShareAction shareAction;
    private String id;

    public ShareDialog(Activity activity, @NonNull ShareInfo shareInfo, @Nullable SHARE_MEDIA[] shareMedias, UMShareListener umShareListener) {
        super(activity, R.style.bottomDialog);
        this.activity = activity;
        this.umShareListener = umShareListener;
        this.shareMedias = shareMedias;
        shareAction = new ShareAction(activity);
        id = shareInfo.id;
        umWeb = new UMWeb(shareInfo.targetUrl);
        umWeb.setTitle(TextUtils.isEmpty(shareInfo.title) ? activity.getString(R.string.app_name)  : shareInfo.title);
        umWeb.setDescription(shareInfo.text);
        UMImage image = new UMImage(activity, shareInfo.imgUrl);
        umWeb.setThumb(image);
        shareAction.withMedia(umWeb);
        init();
    }

    private void init() {
        View v = LayoutInflater.from(activity).inflate(R.layout.dialog_share, null);
        layContent = (LinearLayout) v.findViewById(R.id.lay_content);
        btnCancle = (Button) v.findViewById(R.id.btn_cancle);
        vDismiss = v.findViewById(R.id.v_dismiss);
        vDismiss.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        v.setOnClickListener(this);
        if (shareMedias == null) {
            shareList.add(new ShareItem(SHARE_MEDIA.QQ));
            shareList.add(new ShareItem(SHARE_MEDIA.WEIXIN));
            shareList.add(new ShareItem(SHARE_MEDIA.WEIXIN_CIRCLE));
            shareList.add(new ShareItem(SHARE_MEDIA.QZONE));
        } else {
            for (SHARE_MEDIA media : shareMedias) {
                shareList.add(new ShareItem(media));
            }
        }

        for (ShareItem shareItem : shareList) {
            layContent.addView(createItemView(shareItem));
        }

        setContentView(v);
    }

    private View createItemView(final ShareItem shareItem) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_share, layContent, false);
        ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        TextView tvName = (TextView) view.findViewById(R.id.tv_name);
        ivIcon.setBackgroundResource(shareItem.resId);
        tvName.setText(shareItem.name);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Properties properties = new Properties();
                properties.setProperty("type",shareItem.shareMedia.toString());
//                properties.setProperty("id",id);
                ActivityInfoStrings.sendNormalEvent(ActivityInfoStrings.SHARE_CLICK, properties);
                shareAction.setPlatform(shareItem.shareMedia).setCallback(umShareListener).share();
                dismiss();
            }
        });
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_dismiss:
                dismiss();
                break;
            case R.id.btn_cancle:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    class ShareItem {
        SHARE_MEDIA shareMedia;
        String name;
        int resId;

        ShareItem(SHARE_MEDIA shareMedia) {
            this.shareMedia = shareMedia;
            switch (shareMedia) {
                case WEIXIN:
                    name = activity.getString(R.string.wechat);
                    resId = R.drawable.umeng_socialize_wechat;
                    break;
                case QQ:
                    name = activity.getString(R.string.qq);
                    resId = R.drawable.umeng_socialize_qq;
                    break;
                case QZONE:
                    name = activity.getString(R.string.qzone);
                    resId = R.drawable.umeng_socialize_qzone;
                    break;
                case WEIXIN_CIRCLE:
                    name = activity.getString(R.string.wechat_circle);
                    resId = R.drawable.umeng_socialize_wxcircle;
                    break;
            }
        }
    }
}
